﻿using MCMesServer.DBFunctions.DBFuncs;
using MesDAL.DTO;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCMesServer.WorkForms.Pages
{
    public partial class Frm_MachineStatusShow : UIPage
    {
        List<UseCtr_MachineState> _UseCtrMachineStateList = new List<UseCtr_MachineState>();

        public Frm_MachineStatusShow(Dictionary<string, string> ServerAddress, Dictionary<string, IntPtr> ServerLink)
        {
            InitializeComponent();
            PageInitialize();
        }

        private void PageInitialize()
        {
            SelectMachineState();
        }

        /// <summary>
        /// 查询服务器上设备的状态
        /// </summary>
        private async void SelectMachineState()
        {
            List<MachineInfoDTO> machine_list = await MachineInfoDBFunc.SelectFormMachineStatus();
            for (int i = 0; i < machine_list.Count; i++)
            {
                _UseCtrMachineStateList.Add(new UseCtr_MachineState(machine_list[i]));
            }

            for (int i = 0; i < _UseCtrMachineStateList.Count; i++)
            {
                flowPanel_machineState.Controls.Add(_UseCtrMachineStateList[i]);
                _UseCtrMachineStateList[i].UpdateShower(machine_list);
            }
        }

        /// <summary>
        /// 实时更新设备状态显示
        /// </summary>
        private async void UpdateMachineState()
        {
            List<MachineInfoDTO> machineList = await MachineInfoDBFunc.SelectFormMachineStatus();
            for (int i = 0; i < _UseCtrMachineStateList.Count; i++)
            {
                _UseCtrMachineStateList[i].UpdateShower(machineList);
            }
        }

        private void timer_select_Tick(object sender, EventArgs e)
        {
            UpdateMachineState();
        }

    }
}
